﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BI.CF.Model;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using BI.CF.PageList;
using BI.CF.IDAL;


namespace BI.CF.DAL
{
    public class Record_DAL : Record_IDAL
    {
        /// <summary>
        /// 抵押资产审核显示
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="CrefiticationTimeMax"></param>
        /// <param name="CrefiticationTimeMin"></param>
        /// <param name="IdCard"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public PageList.Pager<Records> GetRecordShow(string UserName, int PageIndex, int PageSize)
        {
            SqlParameter[] Parmeter = { new SqlParameter("@UserName", SqlDbType.VarChar),                         
                                     new SqlParameter("@PageIndex",SqlDbType.Int),
                                     new SqlParameter("@PageSize",SqlDbType.Int)
                                    };
            Parmeter[0].Value = UserName;
            Parmeter[1].Value = PageIndex;
            Parmeter[2].Value = PageSize;
            List<Records> li = new List<Records>();
            int totalcount = 0;
            DataTable Read = DbHelperSQL.RunProcedure("Proc_RecordSelect", Parmeter, PageSize, PageIndex, out totalcount);
            for (int i = 0; i < Read.Rows.Count; i++)
            {
                Records m = new Records();
                m.TID = Convert.ToInt32(Read.Rows[i]["TID"].ToString());
                m.UserId = int.Parse(Read.Rows[i]["UserId"].ToString());
                m.UserName = Read.Rows[i]["UserName"].ToString();
                m.PT_Money = decimal.Parse(Read.Rows[i]["PT_Money"].ToString());
                m.InterestRate = decimal.Parse(Read.Rows[i]["InterestRate"].ToString());
                m.AssetsCertification = Read.Rows[i]["AssetsCertification"].ToString();
                m.Status = Convert.ToBoolean(Read.Rows[i]["Status"]);
                li.Add(m);
            }
            Pager<Records> show = new Pager<Records>();
            show.Data = li;
            show.PageIndex = PageIndex;
            show.PageSize = PageSize;
            show.PageCount = (int)Math.Ceiling(Convert.ToDecimal(totalcount) / Convert.ToDecimal(PageSize));
            return show;
        }
        /// <summary>
        /// 添加抵押资产认证审核信息
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public bool Record_ADD(RecordModel m)
        {
            SqlParameter[] Parameter = { new SqlParameter("@UserId", SqlDbType.Int),
                                       new SqlParameter("@PT_Money",SqlDbType.Decimal),
                                       new SqlParameter("@InterestRate",SqlDbType.Decimal),
                                       new SqlParameter("@AssetsCertification",SqlDbType.VarChar),
                                       new SqlParameter("@MemLoanId",SqlDbType.Int),
                                       new SqlParameter("@Status",SqlDbType.Bit)
                                     };
            Parameter[0].Value = m.UserId;
            Parameter[1].Value = m.PT_Money;
            Parameter[2].Value = m.InterestRate;
            Parameter[3].Value = m.AssetsCertification;
            Parameter[4].Value = m.MemLoanId;
            Parameter[5].Value = m.Status;
            return DbHelperSQL.RunProcedureReturnBool("PROC_RecordAdd", Parameter);
        }
        /// <summary>
        /// 删除抵押资产审核信息
        /// </summary>
        /// <returns></returns>
        public bool Record_Dele(int id)
        {
            SqlParameter[] Parameter = { new SqlParameter("@TID", SqlDbType.Int) };
            Parameter[0].Value = id;
            return DbHelperSQL.RunProcedureReturnBool("PROC_RecordDelete", Parameter);
        }
        /// <summary>
        /// 修改抵押资产审核信息
        /// </summary>
        /// <returns></returns>
        public bool Record_Upt(Records m)
        {
            SqlParameter[] Parameter = { new SqlParameter("@TID", SqlDbType.Int),
                                      
                                       new SqlParameter("@Status",SqlDbType.Bit)
                                     };
            Parameter[0].Value = m.TID;

            Parameter[1].Value = m.Status;
            return DbHelperSQL.RunProcedureReturnBool("PROC_RecordUpdate", Parameter);
        }
        /// <summary>
        /// 根据编号查询状态
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public Records Record_ID(int ID)
        {
            SqlParameter[] Paramter = { new SqlParameter("@TID", SqlDbType.Int) };
            Paramter[0].Value = ID;
            Records m = new Records();
            SqlDataReader dt = DbHelperSQL.RunProcedure("Proc_RecordID", Paramter);
            dt.Read();

            m.Status = Convert.ToBoolean(dt["Status"].ToString());
            return m;
        }
        #region 添加借款信息and 资产抵押
        /// <summary>
        /// 添加借款信息and 资产抵押
        /// </summary>
        /// <returns></returns>
        public bool Memloand_LeiADD(RecordModel m)
        {

            SqlParameter[] Parameter = { new SqlParameter("@LoanMoney",SqlDbType.Decimal),
                                        new SqlParameter("@LoanPurpose",SqlDbType.VarChar),
                                        new SqlParameter("@LoanRaise",SqlDbType.Int),
                                        new SqlParameter("@Annualrate",SqlDbType.Float),
                                        new SqlParameter("@LoanTerm",SqlDbType.Int),
                                        new SqlParameter("@RepaymentModeId",SqlDbType.VarChar),
                                        new SqlParameter("@Title",SqlDbType.VarChar),
                                        new SqlParameter("@LoanExplain",SqlDbType.VarChar),
                                        new SqlParameter("@UserrId",SqlDbType.Int),
                                        new SqlParameter("@LaunchLoanTime",SqlDbType.DateTime),
                                        new SqlParameter("@EndLoanTime",SqlDbType.DateTime),
                                        new SqlParameter("@RepaymentDay",SqlDbType.DateTime),
                                        new SqlParameter("@PayInterest",SqlDbType.Decimal),
                                        new SqlParameter("@RepaySumMoney",SqlDbType.Decimal),
                                        new SqlParameter("@ExamineState",SqlDbType.Int),
                                        new SqlParameter("@AlreadyRaiseLoan",SqlDbType.Decimal),
                                        new SqlParameter("@TotalTransactions",SqlDbType.Int),
                                        new SqlParameter("@LoanType",SqlDbType.VarChar),
                                        new SqlParameter("@AssetsCertification",SqlDbType.VarChar),
                                        new SqlParameter("@Status",SqlDbType.Bit)
                                     };
            Parameter[0].Value = m.LoanMoney;
            Parameter[1].Value = m.LoanPurpose;
            Parameter[2].Value = m.LoanRaise;
            Parameter[3].Value = m.Annualrate;
            Parameter[4].Value = m.LoanTerm;
            Parameter[5].Value = m.RepaymentModeId;
            Parameter[6].Value = m.Title;
            Parameter[7].Value = m.LoanExplain;
            Parameter[8].Value = m.UserrId;
            Parameter[9].Value = m.LaunchLoanTime;
            Parameter[10].Value = m.EndLoanTime;
            Parameter[11].Value = m.RepaymentDay;
            Parameter[12].Value = m.PayInterest;
            Parameter[13].Value = m.RepaySumMoney;
            Parameter[14].Value = m.ExamineState;
            Parameter[15].Value = m.AlreadyRaiseLoan;
            Parameter[16].Value = m.TotalTransactions;
            Parameter[17].Value = m.LoanType;
            Parameter[18].Value = m.AssetsCertification;
            Parameter[19].Value = m.Status;
            return DbHelperSQL.RunProcedureReturnBool("Proc_MemLoanAdd_Lei", Parameter);
        }
        #endregion
    }
}
